console.log('background.js is running');

// background.js
self.addEventListener('fetch', function (event) {
    console.log('Request via:', event.request.mode, event.request.credentials);
    // 如果你有理由相信某个请求应该是由XHR发出的，
    // 但你只看到了fetch事件，你可以推测这个请求可能来自XHR。
});

chrome.webRequest.onCompleted.addListener(
    (detail) => {
        console.log(detail);
    }, { urls: ["https://kunpeng.csdn.net/ad/json/integrate/list*"], types: ["main_frame", "xmlhttprequest"] }, ["responseHeaders"]
)

// Example of a simple user data object
const user = {
    username: 'demo-user'
};

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    console.log('我收到了哈...', message);
    // 2. A page requested user data, respond with a copy of `user`

    sendResponse(user);

    chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
        if (tabs && tabs[0]) {
          const messageToSend = {
            action: "messageFromBackground" + Math.random(), // Define message action for popup.js
            data: "This is a message from the background script!", // Your data
            recipient: tabs[0].id // Store the popup tab ID
          };
          chrome.storage.local.set({ messageForPopup: messageToSend }, function() {
            if (chrome.runtime.lastError) {
              console.error("Error storing message in local storage:", chrome.runtime.lastError.message);
            }
          });
        } else {
          console.warn("Popup is not open, message not sent.");
        }
      });

});

chrome.storage.local.onChanged.addListener(function(changes, namespace) {
    // This function will be called whenever there's a change in chrome.storage.local
    console.log("Storage change detected:", changes);
    // Process the changes object here
  });